package org.ysh.core.attachment;

import org.ysh.annotation.AdditionInfoResolver;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.UUID;

/**
 * 图片附加信息实现类
 */
@AdditionInfoResolver(extensions = {"bmp","jpg","jpeg","tif","tiff","png","gif"})
public class ImageAdditionInfoResolver implements AttachmentAdditionInfoResolver {

    @Override
    public void addAdditionInfo(File tmpFile, AdditionInfo additionInfo) throws Exception {
        System.out.println("修改前文件长度: " + tmpFile.length());
        try(RandomAccessFile raf = new RandomAccessFile(tmpFile.getAbsolutePath(),"rw")){
            raf.seek(raf.length());
            byte[] hostBytes = additionInfo.getHost().getBytes();
            byte[] userNameBytes = additionInfo.getUserName().getBytes();
            long time = additionInfo.getTime().getTime();
            ByteBuffer buffer = ByteBuffer.allocate(1 + hostBytes.length + 1 + userNameBytes.length + 8);

            buffer.put(userNameBytes);
            buffer.put((byte)userNameBytes.length);
            buffer.put(hostBytes);
            buffer.put((byte)hostBytes.length);
            buffer.putLong(time);
            buffer.flip();
            byte[] arr = buffer.array();
            raf.write(arr);
        }
        System.out.println("修改后文件长度： " + tmpFile.length());
    }

    @Override
    public AdditionInfo readAdditionInfo(InputStream in) throws Exception {
        String tmp = "./" + UUID.randomUUID();
        int len = in.available();
        Files.copy(in, Paths.get(tmp));

        try(RandomAccessFile raf = new RandomAccessFile(tmp,"rw")){
            raf.seek(len-8);
            long time = raf.readLong();
            AdditionInfo additionInfo = new AdditionInfo();
            additionInfo.setTime(new Date(time));

            raf.seek(len-9);
            byte hostLen = raf.readByte();
            raf.seek(len-9-hostLen);
            byte[] hostBytes = new byte[hostLen];
            raf.read(hostBytes);
            additionInfo.setHost(new String(hostBytes));

            raf.seek(len-10-hostLen);
            byte userLen = raf.readByte();
            raf.seek(len-10-hostLen-userLen);
            byte[] userBytes = new byte[userLen];
            raf.read(userBytes);
            additionInfo.setUserName(new String(userBytes));

            System.out.println(additionInfo);
            return additionInfo;
        }
    }

    public static void main(String[] args) throws Exception {
        //32
        ImageAdditionInfoResolver imageAdditionInfo = new ImageAdditionInfoResolver();
        AdditionInfo additionInfo = new AdditionInfo();
        additionInfo.setHost("www.baidu.com");
        additionInfo.setUserName("Liyanhong");
        additionInfo.setTime(new Date());
        imageAdditionInfo.addAdditionInfo(new File("D:\\迅雷下载\\clash.txt"),additionInfo);
        imageAdditionInfo.readAdditionInfo(Files.newInputStream(Paths.get("D:\\迅雷下载\\clash.txt")));
//        System.out.println("Liyanhong".getBytes().length);
//        System.out.println("www.baidu.com".getBytes().length);
    }

}
